/*
day318 
2020.7.11 
*/
#include<iostream>
#include<vector>
using namespace std;
 
class Solution {
public:
    int arrayNesting(vector<int>& nums) {
    	int res=0;
		for(int i=0;i<nums.size();++i)
		{
			if(nums[i]!=-1)
			{
				int tem=help(nums,i);
				res=max(res,tem);
			}
		}
		return res;
    }
    
    int help(vector<int> &nums,int point)
    {
    	int res=0;
    	while(point!=-1)
    	{	
//    		cout<<point<<'\t';
    		res++;
			int tem=point;
    		point=nums[point];
    		nums[tem]=-1;
		}
//		cout<<endl;
		return res-1;
	}
};

int main()
{
	vector<int> A = {5,4,0,3,1,6,2};
	Solution s;
	cout<<s.arrayNesting(A)<<endl;
	return 0;
}
